<?php

class Minvitation
{
	private $_db;
	/**
	 *构造函数,初始化数据库连接
	 */
	public function __construct()
	{
		$this->_db = Db::getInstance();
	}
	
	/**
	 *查找聚会发起者选择的可选日期
	 *@param $gatherId 聚会id
	 *@return $optionalDate 可选择日期
	 */
	public function getOptionalDate($gatherId)
	{
		$sql = "select holdDate from gather_date where gatherId = $gatherId";
		try
		{
			$optionalDate = $this->_db->fetch_all($sql);
		}
		catch(Exception $ex)
		{
			return false;
		}
		return $optionalDate;
	}
	
	/**
	 *用户名密码验证
	 *@param $userTel 用户手机号
	 *@param $userPwd 用户密码
	 *@return boolean  
	 */
	public function authenticateUser($userTel, $userPwd)
	{
		$commit = false;
		$sql = "select * from `account` where `tel`='{$userTel}' and `password`='{$userPwd}'";
		$result = $this->_db->num_rows($sql);
		return $result>0 ? true : false;	
	}
	
	/**
	 *查找聚会初始化信息
	 *@param $gatherId
	 *@return $initializeInform
	 */
	public function getInitializeInform($gatherId)
	{
		try
		{
			$sql = "select name,theme,startTime from gather where gatherId = $gatherId";
			$initializeInform = $this->_db->fetch_one($sql);
			$sql = "select max(holdDate) as LastDate,min(holdDate) as FirstDate from gather_date group by gatherId having gatherId = $gatherId";
			$holdTime = $this->_db->fetch_one($sql);
			if(count($initializeInform)>0 && count($holdTime>0))
				$initializeInform = array_merge($initializeInform,$holdTime);
		}
		catch(Exception $ex)
		{
			return false;
		}
		return $initializeInform;
	}
	
	/**
	 *插入受邀请人所选定的日期
	 *@param $userTel
	 *@param $gatherId
	 *@param $value
	 */
	public function insertFreeDate($userTel,$gatherId,$value)
	{
		try
		{
			$sql = "insert into gather_attend(gatherId,attendTel,freeDate) values($gatherId,$userTel,'$value')";
			$this->_db->query($sql);
		}
		catch(Exception $ex)
		{
			return false;
		}
	}
	
	public function deleteFreeDate($userTel,$gatherId,$value)
	{
		try
		{
			$sql = "delete from gather_attend where `gatherId`=$gatherId and `attendTel`=$userTel and `freeDate`='$value'";
			$this->_db->query($sql);
		}
		catch(Exception $ex)
		{
			return false;
		}
	}
	/**
	 *对应添加聚会日期的参加人员数量
	 *@param $gatherId
	 *@param $date
	 */
	public function addAttendNum($gatherId,$date)
	{
		try
		{
			$sql = "update gather_date set attendNum = attendNum + 1 where gatherId = $gatherId and holdDate = '$date'";
			$this->_db->query($sql);
		}
		catch(Exception $ex)
		{
			return false;
		}
	}
	
	public function delAttendNum($gatherId,$date)
	{
		try
		{
			$sql = "update gather_date set attendNum = attendNum - 1 where gatherId = $gatherId and holdDate = '$date'";
			$this->_db->query($sql);
		}
		catch(Exception $ex)
		{
			return false;
		}
	}
	
	public function getLastSelect($gatherId,$userTel)
	{
		try
		{
			$sql = "select `freeDate` from `gather_attend` where `gatherId`={$gatherId} and `attendTel`={$userTel}";
			$lastSelect = $this->_db->fetch_all($sql);
		}
		catch(Exception $ex)
		{
			return false;
		}
		return $lastSelect;
	}
	
	public function getPeople($gatherId)
	{
		try
		{
			$sql = "select `attendName` from `gather_user` where `gatherId`={$gatherId}";
			$result = $this->_db->fetch_all($sql);
			$people = array();
			foreach($result as $key=>$value)
			{
				array_push($people,$value['attendName']);
			}
			return $people;
		}
		catch(Exception $ex)
		{
			return false;
		}
	}
	
	public function updateState($gatherId,$userTel)
	{
		$sql = "update `gather_user` set `state`=1 where `gatherId`=$gatherId and `attendTel`=$userTel";
		$this->_db->query($sql);
	}
	public function updateNoGo($gatherId,$userTel,$lastSelect)
	{
		try
		{
			$sql = "update `gather_user` set `state`=2 where `gatherId`=$gatherId and `attendTel`=$userTel";
			$this->_db->query($sql);
			foreach($lastSelect as $key =>$value)
			{
				if($value=="")
					continue;
				else{
					$sql = "delete from `gather_attend` where `gatherId`=$gatherId and `attendTel`=$userTel";
					$this->_db->query($sql);
					$sql = "update `gather_date` set attendNum = attendNum - 1 where `gatherId`=$gatherId and `holdDate`='$value'";
					$this->_db->query($sql);
				}
			}
		}
		catch(Exception $ex)
		{
			
		}
	}
	
}
?>